package com.Action;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.Bean.wd_orderModel;
import com.Bean.wdcontractModel;
import com.Service.contractService;
import com.Service.orderService;





@Controller
@RequestMapping("/contract")
public class ContractAction {
	@Autowired
	private contractService service;
	@Autowired
	private orderService service1;
	 @InitBinder  
	 public void initBinder(WebDataBinder binder) {  
	     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
	     dateFormat.setLenient(false);  
	     binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); 
	 }
	
	
	@RequestMapping("/toList.do")
	public String getList(Model model,wdcontractModel bean){
    System.out.println("1");
		List<wdcontractModel> list = service.getList(bean);
		model.addAttribute("list", list);
		
		System.out.println("====================================="+list);
//		System.out.println(model);
		return "listContract";
	}
	
	@RequestMapping("/getList.do")
	public String selectList(Model model,wdcontractModel bean){
    System.out.println("2");
    if(bean.getSecondParty().equals("")){
    	bean.setSecondParty(null);
    }
		List<wdcontractModel> list = service.selectList(bean);
		model.addAttribute("list", list);
		model.addAttribute("name", bean.getSecondParty());
		System.out.println("++++++++++++++++++++++++++++++"+list);
//		System.out.println(model);
		return "listContract";
	}
	
	@RequestMapping("/toAdd.do")
	private String toAdd(String orderId,Model model){
		
		System.out.println("000000000"+orderId);
		
		wd_orderModel bean1 = service1.selectByPrimaryKey(orderId);
		System.out.println("66666"+bean1);
		
		model.addAttribute("order", bean1);
		return "addContract";
	}
    
	@RequestMapping("/add.do")
	private String Add(wdcontractModel record){
		System.out.println("9999999"+record);
		int i=service.insert(record);
		System.out.println(i);
		return "redirect:toList.do";
	}

	@RequestMapping("/delete.do")
	public String delete(String contractId){
		int i = service.deleteByPrimaryKey(contractId);
		//System.out.println(i+"kkkkkkkkkk");
		return "redirect:toList.do";
	}
	
	@RequestMapping("/toUpdate.do")
	public String toUpdate(String contractId,Model model){
		wdcontractModel bean = service.selectByPrimaryKey(contractId);
		System.out.println("000000000"+bean.getOrderId());
		
		wd_orderModel bean1 = service1.selectByPrimaryKey(bean.getOrderId());
		System.out.println("66666"+bean1);
		model.addAttribute("contract", bean);
		model.addAttribute("order", bean1);
		
		System.out.println("22222222"+bean);
		return "updateContract";
	}
	
	@RequestMapping("/update.do")
	public String update(wdcontractModel bean){
		if(bean==null){
			System.out.println("kong#######################");
		}else{
		System.out.println("1111111111"+bean);
		int i = service.updateByPrimaryKey(bean);
		
		System.out.println("wwwwwwwwwww"+i);
		}
		return "redirect:toList.do";
	}
	
}